package com.tpsix.util;

import com.tpsix.security.SecurityUserDetails;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


@UtilityClass
public class SecurityUtils {

    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    @SneakyThrows
    public SecurityUserDetails getUser() {
        SecurityUserDetails userDetails;
        try {
            userDetails = (SecurityUserDetails) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new Exception("登陆状态已过期");
        }
        return userDetails;
    }

    public int getUserId() {
        return getUser().getUserId();
    }

}
